package wnhz.com.business.shareshop.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.shareshop.bean.MyRebateBean;
import wnhz.com.business.uitls.ImageLoaderUtil;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

import static com.umeng.socialize.utils.ContextUtil.getContext;

/**
 * 二维码界面
 */
public class TwoWeiMaActivityS extends MyBaseActivity implements ActionBarClickListener {

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.two_wei_mas_head_img)
    ImageView two_wei_mas_head_img;
    @BindView(R.id.two_wei_mas_myname)
    TextView two_wei_mas_myname;
    @BindView(R.id.two_wei_mas_yaoqingma)
    TextView two_wei_mas_yaoqingma;
    @BindView(R.id.two_wei_mas_webview)
    WebView two_wei_mas_webview;

    String codeUrl;

    public static Intent createIntent(Context context, String codeUrl) {
        Intent intent = new Intent(context, TwoWeiMaActivityS.class);
        intent.putExtra("codeUrl",codeUrl);
        return intent;
    }

    @Override
    protected void handleMsg(Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two_wei_mas);
        ButterKnife.bind(this);
        codeUrl =  getIntent().getStringExtra("codeUrl");
        initData();
        initView();
    }

    private void initView() {
        actionbar.setData("我的二维码", R.drawable.ic_lefft_white, null, 0, null, 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }

        //支持App内部javascript交互
        two_wei_mas_webview.getSettings().setJavaScriptEnabled(true);
        //自适应屏幕
        two_wei_mas_webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        two_wei_mas_webview.getSettings().setLoadWithOverviewMode(true);
    }

    private void initData() {
        showDialog("获取中...");
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        XUtil.Post(Url.Ucenter_myCode, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    Log.e("---我的二维码:  ", result);
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    String info = jsonObject.optString("info");
                    if(re.equals("1")){
                        JSONObject object = jsonObject.getJSONObject("info");
                        String invite = object.getString("invite");
                        String code = object.getString("code");
                        String username = object.getString("username");
                        String head_img = object.getString("head_img");
                        ImageLoaderUtil.displayImage(getContext(),two_wei_mas_head_img,head_img,null);//头像
                        two_wei_mas_myname.setText(username);
                        two_wei_mas_yaoqingma.setText("我的邀请码：" + invite);
                        two_wei_mas_webview.loadUrl(code); //关于共享商城网站
                    }else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }
}
